﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;

namespace HomeDMSObjectModel
{
    [Table(Name="Tags")]
    public class Tag
    {
        [Column(Name="tag_name",DbType="nvarchar(100)")]
        public virtual string Name { get; set; }

        public virtual IList<Document> Documents
        {
            get
            {
                HomeDMSContext db = DBContextFactory.GetCurrentContext() as HomeDMSContext;
                if (db != null)
                {
                    var q = from assoc in db.documentTags
                            where assoc.TagId == this.TagId
                            from doc in db.documents
                            where doc.DocumentId == assoc.DocumentId
                            select doc;                            
                    return q.ToList<Document>();
                }
                else
                {
                    return new List<Document>();
                }
            }
        }

        [Column(Name="tag_id",DbType="int identity(1,1)",IsPrimaryKey=true,IsDbGenerated=true)]
        public int TagId { get; set; }

        public override string ToString()
        {
            return Name;
        }

        public override bool Equals(object obj)
        {
            Tag t = obj as Tag;
            if (t == null)
                return false;
            else
                return t.TagId == this.TagId;
        }

        public override int GetHashCode()
        {
            return this.TagId.GetHashCode();
        }
    }
}
